package com.spring.security.uaa.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author luyanan
 * @since 2020/7/30
 * <p>web安全配置</p>
 **/
@Configuration
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {


    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }


    // 安全拦截机制
    @Override
    protected void configure(HttpSecurity http) throws Exception {

//        http.csrf().disable()
//                .authorizeRequests()
//
//                .antMatchers("/login").permitAll()
//                .anyRequest().authenticated()
//                .and().formLogin();
        http.requestMatchers().antMatchers("/login", "/oauth/authorize")
                .and()
                .authorizeRequests()
                .anyRequest().authenticated()
//                    .antMatchers("/logout**").permitAll()
//                    .antMatchers("/**").hasAnyRole("ROLE_ADMIN", "ROLE_USER")
                .and()
                .formLogin().loginPage("/login").permitAll()
//            .and()
//                .logout()
//                    .logoutSuccessUrl("/logout_success")
//                    .invalidateHttpSession(true)
//                    .permitAll()
                .and()
                .csrf().disable();
    }


}
